package com.qianfeng.openplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openplatform.commons.field.RedisKeyPrefix;
import com.qianfeng.openplatform.webmaster.client.CacheClient;
import com.qianfeng.openplatform.webmaster.entry.LayuiDataTableSource;
import com.qianfeng.openplatform.webmaster.entry.OpenApiMaster;
import com.qianfeng.openplatform.webmaster.mapper.AppInfoMapper;
import com.qianfeng.openplatform.webmaster.pojo.AppInfo;
import com.qianfeng.openplatform.webmaster.pojo.AppInfoExample;
import com.qianfeng.openplatform.webmaster.pojo.Customer;
import com.qianfeng.openplatform.webmaster.service.AppInfoService;
import com.qianfeng.openplatform.webmaster.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 江江
 * @Date 2020/7/23 11:02
 */
@Service
@Transactional
public class AppInfoServiceImpl implements AppInfoService {
    @Autowired
    private AppInfoMapper appInfoMapper;
    @Autowired
    private CustomerService customerService;

    @Autowired //签名校验md5
    private CacheClient cacheClient;

    @Override
    public LayuiDataTableSource getApiInfoMgsToPage(Integer page, Integer limit, String corpName) {
        LayuiDataTableSource source = new LayuiDataTableSource();
        PageHelper.startPage(page, limit);
        AppInfoExample example = new AppInfoExample();
        AppInfoExample.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(corpName)) {
            c.andCorpnameLike(corpName);
        }
        List<AppInfo> appInfoList = appInfoMapper.selectByExample(example);
        PageInfo<AppInfo> info = new PageInfo<>(appInfoList);
        source.setCount(info.getTotal());
        source.setData(info.getList());
        return source;
    }

    @Override
    public OpenApiMaster addAppinfo(AppInfo appInfo) {

        int customerId = appInfo.getCusid();
        Customer customer = customerService.getCustomerById(customerId);
        appInfo.setCorpname(customer.getNickname());
        int code = appInfoMapper.insert(appInfo);
        if (code > 0) {
            try {
                cacheClient.hMset(RedisKeyPrefix.WEB_MASTER_API_SERVICE_APPKEY_KEY+appInfo,appInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("添加失败!!");
    }

    @Override
    public OpenApiMaster updateAppinfo(AppInfo appInfo) {
        int customerId = appInfo.getCusid();
        Customer customer = customerService.getCustomerById(customerId);
        appInfo.setCorpname(customer.getNickname());
        int code = appInfoMapper.updateByPrimaryKey(appInfo);
        if (code > 0) {
        try {
            if (appInfo.getState()==1){
                cacheClient.hMset(RedisKeyPrefix.WEB_MASTER_API_SERVICE_APPKEY_KEY + appInfo.getAppkey(), appInfo);
            } else {
                cacheClient.deleteKey(RedisKeyPrefix.WEB_MASTER_API_SERVICE_APPKEY_KEY+appInfo.getAppkey());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("更改错误!!");
    }
}
